<template>
    <div style="-webkit-app-region: drag;">
        <div class="container">
            <el-card class="card">
                <div>
                    <h1>登录-咸鱼云</h1>
                    <el-form
                        style="-webkit-app-region: no-drag;"
                        ref="form"
                        label-width="80px"
                    >
                        <el-form-item label="您的账号">
                            <el-input v-model="form.account"></el-input>
                        </el-form-item>
                        <el-form-item label="您的密码">
                            <el-input v-model="form.password" type="password"></el-input>
                        </el-form-item>
                        <div>
                            <el-button type="primary" @click="login"
                                >立即登录</el-button
                            >
                        </div>
                    </el-form>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script>
import fs from 'fs'
import { remote } from 'electron'
import path from 'path'
import API from '../../api'
export default {
    data() {
        return {
            form: {
                account: '游客',
                password: '123456'
            }
        }
    },
    mounted() {
        this.readConfig()
    },
    methods: {
        readConfig() {
            // 读取配置文件，变量config的类型是Object类型
            let config = null
            const filePath = path.join(remote.app.getPath('userData'), 'saltedfishcloud-config.json')
            console.log(filePath)
            try {
                config = JSON.parse(fs.readFileSync(filePath).toString())
                this.form = config
                console.log(this.$route.params.logout)
                if (this.$route.params.logout) {
                    return
                }
                this.login()
            } catch (err) {
                console.log('没有默认账号密码。')
            }
            console.log(config)
        },
        saveConfig() {
            try {
                const filePath = path.join(remote.app.getPath('userData'), 'saltedfishcloud-config.json')
                fs.writeFileSync(filePath, JSON.stringify(this.form))
                // 文件写入成功。
            } catch (err) {
                console.error(err)
            }
        },
        async login() {
            if (this.form.account == '游客') {
                this.$router.push('/common/publicdisk')
            } else {
                this.login_go()
            }
        },
        async login_go() {
            const res = await this.axios(API.user.login(this.form.account, this.form.password))
            console.log(res)
            const token = res.data.data
            if (token == null) {
                this.$alert('用户名或密码错误', '产生了一段爱情', {
                    confirmButtonText: '确定'
                })
            } else {
                const username = this.form.account
                const avatarURL = `${API.getServer()}${API.user.getAvatar(this.form.account).url}`
                let userInfo = { token, avatarURL, username }
                this.$store.commit('setUserInfo', userInfo)
                const res = await this.axios(API.user.getUserInfo())
                userInfo = { ...userInfo, ...res.data.data }
                console.log(userInfo)
                this.$store.commit('setUserInfo', userInfo)
                this.$router.push('/common/publicdisk')
            }
        }
    }
}
</script>

<style lang="less" scoped>
.container {
    background-color: rgba(240, 248, 255, 0);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 500px;
}
.card {
    width: 400px;
    .xx-code {
        display: flex;
        flex-direction: row;
    }
}
</style>
